<template>
  <div class="role-div">
    <el-form :model="form" label-width="70px" size="small">
      <el-form-item label="角色名称">
        <el-input
          style="width: 100%"
          placeholder="角色名称"
          v-model="form.keywords"
        />
      </el-form-item>
      <el-row style="display: flex; margin-bottom: 10px">
        <el-button type="primary" @click="loadData" size="small">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>
    <!-- 添加按钮 -->
    <div class="post-div">
      <el-button size="small" type="success" @click="addShow">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="roleList" style="width: 100%">
      <el-table-column prop="roleName" label="角色名称" width="180" />
      <el-table-column prop="roleCode" label="角色Code" width="180" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
          <el-button
            type="warning"
            size="small"
            @click="handleAssign(scope.row)"
          >
            分配菜单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination -->
    <el-pagination
      v-model:current-page="pagination.pageNum"
      v-model:page-size="pagination.pageSize"
      :page-sizes="[1, 2, 3, 10]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="loadData"
      @current-change="loadData"
      style="margin-top: 20px"
    />
    <!-- 弹框 -->
    <el-dialog v-model="dialogFormVisible" title="新增角色">
      <el-form :model="form1">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form1.roleName" autocomplete="off" />
        </el-form-item>

        <el-form-item label="角色code" label-width="120px">
          <el-input v-model="form1.roleCode" autocomplete="off" />
        </el-form-item>

        <el-form-item label="角色描述" label-width="120px">
          <el-input v-model="form1.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveOrUpdate">确认</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 树形复选框 -->
    <el-dialog v-model="dialogTreeCheckbox" title="分配菜单">
      <el-tree
        :data="menus"
        show-checkbox
        node-key="id"
        default-expand-all="true"
        :default-checked-keys="checkedKeys"
        :props="defaultProps"
        ref="menuParam"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogTreeCheckbox = false">取消</el-button>
          <el-button type="primary" @click="handleSaveMenu">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import {
  GetRoleList,
  DeleteRole,
  SaveRole,
  UpdateRole,
  GetMenusByRoleId,
  AssignMenus,
} from '@/api/sysRole.js'
import { GetMenuList } from '@/api/sysMenu'
import { ElMessage, ElMessageBox } from 'element-plus'
//查询参数
const selectParam = reactive({
  pageNum: 0,
  pageSize: 0,
  keywords: '',
})
//弹窗表单数据
const form1 = ref({
  roleName: '',
  roleCode: '',
  description: '',
  id: 0,
})
const form2 = {
  roleName: '',
  roleCode: '',
  description: '',
  id: 0,
}
//页面挂载时加载数据
const loadData = async () => {
  selectParam.pageNum = pagination.pageNum
  selectParam.pageSize = pagination.pageSize
  selectParam.keywords = form.value.keywords
  const { data } = await GetRoleList(selectParam)
  console.log(data)
  roleList.value = data.list
  pagination.total = data.total
}
onMounted(() => {
  loadData()
})
//弹框
const title = ref('新增角色')
const dialogFormVisible = ref(false)
// 表单
const form = ref({ keywords: '' })
//重置按钮点击事件
const resetData = () => {
  form.value.keywords = ''

  loadData()
}
// 表格
const roleList = ref([])
//分页器
const pagination = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
})
//删除指定角色
const handleDelete = row => {
  ElMessageBox.confirm(`确认删除角色【${row.roleName}】吗?`, 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      await DeleteRole(row.id)
      loadData()
    })
    .catch(() => {
      ElMessage({
        type: 'success',
        message: '取消删除',
      })
    })
}
//添加角色||修改角色
//添加角色
const addShow = () => {
  dialogFormVisible.value = true
  form1.value = form2
  title.value = '新增角色'
}
//修改角色
const handleEdit = async row => {
  dialogFormVisible.value = true
  // form1.value = row
  Object.assign(form1.value, row)
  title.value = '修改角色'
}
const handleSaveOrUpdate = async () => {
  if (form1.value.id === 0) {
    await SaveRole(form1.value)
    dialogFormVisible.value = false
    loadData()
    ElMessage({
      type: 'success',
      message: '新增成功',
    })
  } else {
    await UpdateRole(form1.value)
    dialogFormVisible.value = false
    loadData()
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
  }
}
//树形复选框
//给角色回显已存在菜单
const menus = ref([])
const selectRoleId = ref()
const defaultProps = {
  children: 'children',
  label: 'title',
}
const dialogTreeCheckbox = ref(false)
const checkedKeys = ref([])
const handleAssign = async row => {
  checkedKeys.value = []
  menus.value = []
  dialogTreeCheckbox.value = true
  const { data } = await GetMenuList()
  menus.value = data
  selectRoleId.value = row.id
  const response = await GetMenusByRoleId(selectRoleId.value)
  checkedKeys.value = response.data
}
//保存菜单
const menuParam = ref()
const handleSaveMenu = async () => {
  const arr = menuParam.value.getCheckedKeys()
  const arr1 = menuParam.value.getHalfCheckedKeys()
  const selectIdArr = [...arr, ...arr1]
  await AssignMenus(selectRoleId.value, selectIdArr)
  dialogTreeCheckbox.value = false
  ElMessage.success('分配菜单成功')
}
</script>

<style>
.role-div {
  background: #fff;
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
}
.post-div {
  margin: 10px, 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background: #fff;
}
</style>
